package nio.channel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//输出
public class channelDemo_1 {
    public static void main(String[] args) throws IOException {
        String str = "hello,world.";
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("1.txt");
        //获取通道
        FileChannel fileChannel = fileOutputStream.getChannel();
        //缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //数据放入缓冲区
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();//读写切换
        //将缓冲区的数据写入通道
        fileChannel.write(byteBuffer);
        fileChannel.close();
    }

}
